<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class VARecordationAndTransferTaxCalculator{

    private $rateInfoProperties;
    private $titleInsuranceCalculatorHelper;
    private $state;
    private $countyLocales;
    private $taxCalculatorHelper;
    private $feeCalculator;

    public function VARecordationAndTransferTaxCalculator($state, $rateInfoProperties) 
    {
       $this->state = $state;
       $this->rateInfoProperties = $rateInfoProperties;
       
       $this->titleInsuranceCalculatorHelper = new TitleInsuranceCalculatorHelper();
       
       $this->taxCalculatorHelper = new TaxCalculatorHelper();
       
       $this->countyLocales = CountyLocale::getCountyLocales($state);
       $this->feeCalculator = new FeeCalculator($state, $rateInfoProperties);
    }    

    public function supportsCountyLocale() {
        return true;
    }

    public function getCountyLocales() {
        return $this->countyLocales;
    }

    public function supportsTownLocale() {
        return false;
    }

    public function getTownLocales($countyLocale) {
        return array();
    }

    public function calculatePurchaseRecordationAndTransferTax($countyLocale,$townLocale,$consideration,$loanAmounts,$qualifiedFirstTimeBuyer,$ownerOccupiedResidence,$requestedPayer) 
    {

        $loanAmount = $this->taxCalculatorHelper->getLoanTotal($loanAmounts);

        // zero indicates false
        $mortgage = $this->taxCalculatorHelper->getNumberOfLoans($loanAmounts);

        // CT-32 Added fees per State
        $fees = $this->feeCalculator->calculate(true, $mortgage, $ownerOccupiedResidence);

        $stateTax = $this->getStatePropertyTransferTax($consideration);
        $countyTax = $this->getCountyTransferTax($consideration);

        // CT-14 if purchase and loan the state & local tax are additive.
        if ($loanAmount > 0) {
            $stateTax->addTax($this->getStatePropertyTransferTax($loanAmount));
            $countyTax->addTax($this->getCountyTransferTax($loanAmount));
        }

        return RecordationAndTransferTaxCreator::createSplitRecordationAndTransferTaxes(
                $this->state,
                $stateTax, $countyTax, $this->getRecordationTax($consideration),
                0.0,
                $fees,
                $requestedPayer, null);
    }

    public function getHeaderHTML() {
        return "The state and county recordation taxes are typically paid by the buyer; the grantor tax is paid by the seller.";
    }

    public function getFooterHTML() {
        return "<i>To get a \"same-lender exemption\", we will need to present the payoff statement to show the new lender and the payoff lender are the same.</i>";
    }

    public function calculateRefinanceRecordationAndTransferTax($countyLocale, $loanAmounts, $ownerOccupiedResidence,$payoffAmounts,$sameLender) {

        $loanAmount = $this->taxCalculatorHelper->getLoanTotal($loanAmounts);
        $payoffAmount = $this->taxCalculatorHelper->getPayoffTotal($payoffAmounts);

        // VA Refi
        // 2 options
        // 1 new lender - no exemption
        // 2 same lender - exemption for original balance of payoff loan(s).
        if ($sameLender) {
            $loanAmount -= $payoffAmount;
            $loanAmount = max($loanAmount, 0.0);
        }

        return new RecordationAndTransferTaxes(
                $this->getStatePropertyTransferTax($loanAmount), $this->getCountyTransferTax($loanAmount), null,  // CT-14 No recordation tax if no purchase.
                $this->feeCalculator->calculate(false, count($loanAmounts), $ownerOccupiedResidence),
                null);
    }

    protected function getRecordationTax($consideration) {
        $halfThousands = $this->titleInsuranceCalculatorHelper->roundUp2HalfThousands($consideration);

        // Grantee tax applies to both deeds (based on value) and deeds of trust (based on amount of debt). Grantor
        // deed recording tax only applies to deeds other than deeds of trust, and is paid by the grantor. A graduated
        // tax scale applies to deeds of trust with debt over $10 million dollars. Please see State Summary for details.

        // Grantor Deed Recording Tax	.................... $0.50 per $500 or fraction thereof of value
        $tierCosts = array();
        $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.stategrantor.va.rate")/*0.50*/);

        return new RecordationTax("Grantor Deed Recording Tax", $this->titleInsuranceCalculatorHelper->calculateTieredCost($halfThousands, $tierCosts, 0.0));
    }

    protected function getStatePropertyTransferTax($consideration) {

        $hundreds = $this->titleInsuranceCalculatorHelper->roundUp2Hundreds($consideration);

        // Grantee tax applies to both deeds (based on value) and deeds of trust (based on amount of debt). Grantor
        // deed recording tax only applies to deeds other than deeds of trust, and is paid by the grantor. A graduated
        // tax scale applies to deeds of trust with debt over $10 million dollars. Please see State Summary for details.

        // State Grantee Tax	.................... $0.25 per $100 or fraction thereof (see note)
        $tierCosts = array();
        $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.stategrantee.va.rate")/*0.25*/);

        return new StatePropertyTransferTax("State Grantee Tax", $this->titleInsuranceCalculatorHelper->calculateTieredCost($hundreds, $tierCosts, 0.0));
    }

    protected function getCountyTransferTax($consideration) {
        $stateTax = $this->getStatePropertyTransferTax($consideration);

        // Local Grantee Tax	.................... 1/3 of amount of state grantee tax, rounded to nearest penny
        $tax = $stateTax->getAmount() / 3.0;
        $tax = $this->titleInsuranceCalculatorHelper->round2($tax, 2);

        return new CountyPropertyTransferTax("Local Grantee Tax", $tax);
    }
}
?>
